<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Batch Analysis Report</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            line-height: 1.6;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        .header {
            text-align: center;
            margin-bottom: 30px;
            padding: 20px;
            background-color: #f8f9fa;
            border-radius: 5px;
        }
        .summary {
            margin: 20px 0;
            padding: 20px;
            background-color: #f5f5f5;
            border-radius: 5px;
        }
        .stat-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin: 20px 0;
        }
        .stat-box {
            padding: 15px;
            background-color: #fff;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .plot-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(500px, 1fr));
            gap: 20px;
            margin: 20px 0;
        }
        .plot-container {
            text-align: center;
            padding: 15px;
            background-color: #fff;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .plot-container img {
            max-width: 100%;
            height: auto;
            border-radius: 5px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
            background-color: #fff;
        }
        th, td {
            padding: 12px;
            border: 1px solid #ddd;
            text-align: left;
        }
        th {
            background-color: #f8f9fa;
            font-weight: bold;
        }
        tr:nth-child(even) {
            background-color: #f8f9fa;
        }
        tr:hover {
            background-color: #f5f5f5;
        }
        .metric {
            font-size: 1.2em;
            font-weight: bold;
            color: #2c3e50;
        }
        .footer {
            margin-top: 40px;
            padding-top: 20px;
            border-top: 1px solid #ddd;
            text-align: center;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>Colony Counter - Batch Analysis Report</h1>
        <p>Generated on: {{ datetime }}</p>
    </div>

    <div class="summary">
        <h2>Analysis Summary</h2>
        <p><strong>Image:</strong> {{ image_name }}</p>
        <p><strong>Total Runs:</strong> {{ total_runs }}</p>
        <p><strong>Analysis Duration:</strong> {{ duration }}</p>
    </div>

    <div class="stat-grid">
        <div class="stat-box">
            <h3>Colony Count Statistics</h3>
            <p><strong>Mean Count:</strong> <span class="metric">{{ mean_count:.1f}}</span></p>
            <p><strong>Standard Deviation:</strong> {{ std_count:.1f }}</p>
            <p><strong>Coefficient of Variation:</strong> {{ cv:.1f }}%</p>
            <p><strong>Range:</strong> {{ min_count }} - {{ max_count }}</p>
            <p><strong>Median:</strong> {{ median_count }}</p>
        </div>
        
        <div class="stat-box">
            <h3>Confidence Analysis</h3>
            <p><strong>Mean Confidence:</strong> <span class="metric">{{ mean_confidence:.3f }}</span></p>
            <p><strong>Confidence STD:</strong> {{ std_confidence:.3f }}</p>
            <p><strong>Min Confidence:</strong> {{ min_confidence:.3f }}</p>
            <p><strong>Max Confidence:</strong> {{ max_confidence:.3f }}</p>
        </div>
        
        <div class="stat-box">
            <h3>Distribution Analysis</h3>
            <p><strong>Q1 (25th percentile):</strong> {{ q1_count }}</p>
            <p><strong>Q3 (75th percentile):</strong> {{ q3_count }}</p>
            <p><strong>IQR:</strong> {{ iqr_count }}</p>
            <p><strong>Skewness:</strong> {{ skewness:.3f }}</p>
        </div>
    </div>

    <div class="plot-grid">
        <div class="plot-container">
            <h3>Count Distribution</h3>
            <img src="{{ plot_dist_path }}" alt="Count Distribution">
        </div>
        
        <div class="plot-container">
            <h3>Count Sequence</h3>
            <img src="{{ plot_seq_path }}" alt="Count Sequence">
        </div>
        
        {% if plot_corr_path %}
        <div class="plot-container">
            <h3>Correlation Matrix</h3>
            <img src="{{ plot_corr_path }}" alt="Correlation Matrix">
        </div>
        {% endif %}
    </div>

    {% if results_table %}
    <div class="results">
        <h2>Results Summary</h2>
        {{ results_table|safe }}
    </div>
    {% endif %}

    <div class="footer">
        <p>Generated by Colony Counter v{{ version }}</p>
        <p><small>Analysis completed in {{ duration }}</small></p>
    </div>
</body>
</html>
